var http = require("http"),
    mysql = require("mysql"),
    work = require("./lib/timetraker");

var db = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"root",
    database:"test"
});

var server = http.createServer(function(req,res){
    switch (req.method){
        case "POST" :
            switch (req.url){
                case "/":
                    work.add(db,req,res);
                    break;
                case  "/archived":
                    work.archived(db,req,res);
                    break;
                case "/delete" :
                    work.delete(db,req,res);
                    break;
                default :
                    notFound(res);
                    break;
            }
            break;
        case "GET" :
            switch (req.url){
                case "/":
                    work.show(db,res);
                    break;
                case "/archived":
                    work.showArchived(db,res);
                    break;
                default :
                    notFound(res);
                    break;
            }
            break;
    }
});

function notFound(res){
    res.write("The request you sended is not available.<br/>" +
    "go back to the <a href='http://localhost:8899'>index page</a>");
}

db.query('CREATE TABLE IF NOT EXISTS work(' +
'id INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,' +
'hours DECIMAL(5,2) DEFAULT 0,' +
'date DATE,' +
'archived INT(1) DEFAULT 0,' +
'description LONGTEXT)' ,function(err){
    if(err) throw err ;
    console.log("Server Started..");
    server.listen(8899);
});